function px2rad(pxVal, m) {
  const factor = (Math.sqrt(Math.pow(2, (m-2)/m)/Math.pow(m, 2))*Math.abs(m))/Math.abs(-1 + m)
  return pxVal / factor
}

function r3cor(w, h, ...r) {
  console.log("r3cor: " + r)
  const rat = [w/(r[0]+r[1]), h/(r[1]+r[2])]
  const min = Math.min(...rat)
  if (min < 1) {
    const minIndex = rat.indexOf(min)
    r[minIndex] *= min
    r[minIndex + 1] *= min
    let x = (minIndex>0) ? w : h
    if(r[2-2*minIndex] + r[1] > x) {
      r[2-2*minIndex] *= min
    }
  }
  console.log('fin3: ' + r)
  return r
}

function r4cor(w, h, ...r) {
  const rat = [w/(r[0]+r[1]), w/(r[2]+r[3]), h/(r[0]+r[3]), h/(r[2]+r[3])]
  console.log("rat4: " + rat)
  const min = Math.min(...rat)
  console.log(min)
  if (min < 1) {
    const minIndex = rat.indexOf(min)
    const parat = [
      [0, 1],
      [2, 3],
      [0, 3],
      [1, 2]
    ]
    r[parat[minIndex][0]] *= min
    r[parat[minIndex][1]] *= min
    let x = (minIndex>1) ? w : h
    for(let i in parat) {
      if (i == minIndex) {
        continue
      }
      let s1 = parat[i].indexOf(parat[minIndex][0])
      let s2 = parat[i].indexOf(parat[minIndex][1])
      if (s1 !== -1) {
        if (r[parat[i][1-s1]] + r[parat[minIndex][0]] > x) {
          r[parat[i][1-s1]] *=min
        }
      }
      if (s2 !== -1) {
        if (r[parat[i][1-s2]] + r[parat[minIndex][1]] > x) {
          r[parat[i][1-s2]] *=min
        }
      }
    }
  }
  console.log('fin4: ' + r)
  return r
}

export { px2rad, r3cor, r4cor }